package com.xml;

import java.util.ArrayList;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Remark;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.FrameTag;
import org.htmlparser.tags.HeadTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ObjectTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.tags.StyleTag;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;

public class MyNotFilter {

	public MyNotFilter() {

	}

	/**
	 * 过滤掉DIV Frame Css Object
	 * 
	 * @param node
	 */
	public void accept(Node node) {
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode instanceof ScriptTag || aNode instanceof ObjectTag
					|| aNode instanceof StyleTag || aNode instanceof FrameTag) {
				arrayList.add(aNode);
			}
		}
		// 然后删除节点
		for (int i = 0; i < arrayList.size(); i++) {
			Node deletedNode = (Node) arrayList.get(i);
			list.remove(deletedNode);
		}

		// 继续子集合
		size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			accept(aNode);
		}

	}

	/**
	 * 过滤掉<table>,无论table中是否有文本，此时作为2级推荐内容。
	 * 
	 * @param node
	 */
	public void filterTable(Node node) {
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode instanceof TableTag) {
				arrayList.add(aNode);
			}
		}
		// 然后删除节点
		for (int i = 0; i < arrayList.size(); i++) {
			Node deletedNode = (Node) arrayList.get(i);
			list.remove(deletedNode);
		}

		// 继续子集合
		size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			filterTable(aNode);
		}

	}

	public void filterRemark(Node node) {
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode instanceof Remark) {
				if(aNode.getText().indexOf("正文块 begin")==-1 && aNode.getText().indexOf("正文内容 end")==-1) {
				arrayList.add(aNode);
				}
			}
		}
		// 然后删除节点
		for (int i = 0; i < arrayList.size(); i++) {
			Node deletedNode = (Node) arrayList.get(i);
			list.remove(deletedNode);
		}

		// 继续子集合
		size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			filterRemark(aNode);
		}

	}

	public static String getText(Node node) {
		if (node == null) {
			return "";
		}
		NodeList list = node.getChildren();
		if (list == null) {
			return "";
		}
		String content = "";
		NodeList list2 = list.extractAllNodesThatMatch(new NodeClassFilter(
				TextNode.class), true);
		content = list2.toHtml();
		return content;
	}

	// 去掉空内容的节点
	public void filterNULL(Node node) {
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode.getText() == null || aNode.toHtml().trim().equals("")) {
				arrayList.add(aNode);
			}

		}
		// 然后删除节点
		for (int i = 0; i < arrayList.size(); i++) {
			Node deletedNode = (Node) arrayList.get(i);
			list.remove(deletedNode);
		}

		// 继续子集合
		size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			filterNULL(aNode);
		}

	}

	// 一次处理子结点
	public static boolean processChilds(Node node) {
		NodeList list = node.getChildren();
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return false;
		}
		int i = 0;
		for (; i < list.size(); i++) {
			Node aNode = list.elementAt(i);
			boolean flag = deleteByMaxCount(aNode);
			if (flag) {
				list.remove(aNode);
			}
		}

		return false;

	}

	// 取得最大文本字数
	public static boolean deleteByMaxCount(Node node) {
		NodeList list = node.getChildren();
		int maxCount = 0;
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return false;
		}
		NodeFilter nf = new NodeClassFilter(TextNode.class);
		list = list.extractAllNodesThatMatch(nf, true);
		int size = list.size();
		// 取得最大文本字数
		for (int i = 0; i < size; i++) {
			String vtext = list.elementAt(i).getText();
			if (vtext.length() > maxCount) {
				maxCount = vtext.length();
			}
		}

		if (maxCount < 30) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 如果一个p中含有两个以上的a,则去掉p 如果只有一个a，并且没有文本节点，也删除
	 * @param node
	 * @return
	 */

	public void deleteByPLink(Node node) {
		NodeList list = node.getChildren();

		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return;
		}
		ArrayList arrayList = new ArrayList();
		int size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode instanceof ParagraphTag) {

				int size2 = getLinks(aNode);
				int size3 = getTexts(aNode);
				if (size2 > 1) {
					arrayList.add(aNode);
				} else {
					if (size2 == 1 && size3 == 1) {
						// System.out.println(aNode.toHtml());
						arrayList.add(aNode);
					}

				}

			}
		}
		// 然后删除节点
		for (int i = 0; i < arrayList.size(); i++) {
			Node deletedNode = (Node) arrayList.get(i);
			list.remove(deletedNode);
		}

		// 继续子集合
		size = list.size();
		// 先标志需要删除的节点集合
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			deleteByPLink(aNode);
		}

	}

	public static int getLinks(Node node) {
		NodeList list = node.getChildren();
		int maxCount = 0;
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return 0;
		}
		NodeFilter nf = new NodeClassFilter(LinkTag.class);
		list = list.extractAllNodesThatMatch(nf, true);
		int size = list.size();
		return size;
	}

	public static int getTexts(Node node) {
		NodeList list = node.getChildren();
		int maxCount = 0;
		int aCount = 0;
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return 0;
		}
		NodeFilter nf = new NodeClassFilter(TextNode.class);
		list = list.extractAllNodesThatMatch(nf, true);
		int size = list.size();
		for (int i = 0; i < size; i++) {
			Node aNode = list.elementAt(i);
			if (aNode.getText().trim().equals("")
					|| aNode.getText().replaceAll("&nbsp;", "").trim().equals(
							"")) {

			} else {
				aCount++;
			}
		}
		return aCount;
	}
	
	
	public static NodeList getTextNodes(Node node) {
		NodeList list = node.getChildren();
		int maxCount = 0;
		int aCount = 0;
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return null;
		}
		NodeFilter nf = new NodeClassFilter(TextNode.class);
		list = list.extractAllNodesThatMatch(nf, true);
		return list;
	}

	// 判断结点中是否包含table,frame,如果包含则取其中的<p>
	public static boolean f2(Node node) {
		NodeList list = node.getChildren();
		NodeList list2 = null;
		// 如果子结点为空，返回
		if (list == null || list.size() == 0) {
			return false;
		}
		NodeFilter nf = new NodeClassFilter(TableTag.class);
		NodeFilter nf2 = new NodeClassFilter(LinkTag.class);
		list = list.extractAllNodesThatMatch(nf2, true);
		list2 = list.extractAllNodesThatMatch(nf, true);
		int size = list.size();
		// 如果超链接数大于1并且含有table丢弃，
		if (size > 1 && list2.size() > 0) {
			return true;
		}
		return false;
	}
}
